package com.yesep.message;

import com.yesep.constant.Constants;
import com.yesep.handler.MessageHandler;
import com.yesep.model.DataFactor;
import com.yesep.model.DataText;
import com.yesep.model.MessageBaseModel;
import com.yesep.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
 * @program: envm
 * @description: 报文基础抽象类
 * @author: quliang
 * @create: 2019-07-18 11:56
 **/
public abstract class MessageBaseService {
    private static Logger logger = LoggerFactory.getLogger(MessageBaseService.class);

    /**
     * 转换成功消息对象
     *
     * @param messageBaseModel
     * @return
     */
    public MessageBaseModel tranSuccessMessage(MessageBaseModel messageBaseModel) {
        MessageBaseModel responseMessage = new MessageBaseModel();
        responseMessage.setST(messageBaseModel.getST());
        responseMessage.setCN(messageBaseModel.getCN());
        responseMessage.setPW(messageBaseModel.getPW());
        responseMessage.setMN(messageBaseModel.getMN());
        responseMessage.setFlag(String.valueOf(Constants.IS_RESPONSE_NO));
        responseMessage.getCP().put("QN", messageBaseModel.getQN());
        return responseMessage;
    }

    /**
     * 转换生成指标对象
     *
     * @param dataText
     * @param messageBaseModel
     * @return 判空返回null, 否则返回对象
     */
    public DataFactor tranDataFactorFlagFormMessage(DataText dataText, MessageBaseModel messageBaseModel) {
        DataFactor dataFactor = new DataFactor();
        dataFactor.setDtId(dataText.getDtId());
        dataFactor.setSiteId(dataText.getSiteId());
        dataFactor.setDataSt(messageBaseModel.getST());
        dataFactor.setDataCn(messageBaseModel.getCN());
        dataFactor.setDataTime(DateUtils.stringToDate(messageBaseModel.getQN(), DateUtils.TIMESTAMP_NO_SPLASH_FORMAT));
        dataFactor.setParseTime(new Date());
        dataFactor.setDataStatus(Constants.DATA_TEXT_STATUS_SUCCESS);
        dataFactor.setDataType(Constants.DATA_FACTOR_TYPE_NORMAL);

        Class classObj = DataFactor.class;

        Map cpMap = messageBaseModel.getCP();
        if (cpMap.get("DataTime") != null) {// 如果上传采集时间不为空时则重新设置期数据采集时间
            dataFactor.setDataTime(DateUtils.stringToDate(cpMap.get("DataTime").toString(), DateUtils.DATA_TIME_NO_SPLASH_FORMAT));
        }
        Iterator iter = cpMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = (String) entry.getKey();
            String methodName = getSetColumnMethodName(key, "-Flag");
            if (Objects.equals(methodName, null)) return null;
            if (methodName.startsWith("set")) {
                try {
                    Method method = classObj.getMethod(methodName, String.class);
                    method.invoke(dataFactor, (String) entry.getValue());
                } catch (Exception e) {
                    logger.error("设置监测指标状态" + key + "失败", e);
                }
            }
        }

        return dataFactor;
    }


    /**
     * 取得相关的列名
     *
     * @param name
     * @return
     */
    public String getSetColumnMethodName(String name, String type) {
        String setMethodName = "";
        String paramName = "";
        if (name.indexOf(type) != -1) {
            paramName = name.substring(0, name.indexOf(type));
            try {
                //从缓存中取出数据
                String columnNum = null;
//                if (columnNum == null || columnNum.equals(""))
//                    return "";
                if (Objects.equals(columnNum, null)) return null;
                setMethodName = "setF"
                        + MessageHandler.paddingLength(columnNum, 2, 0, "0");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return setMethodName;
        } else {
            return "";
        }
    }

}
